package com.example.myapplication.activity;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.os.Message;
import android.util.Log;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.myapplication.R;

public class NongyeActivity extends BaseActivity {

    @Override
    protected void doHttpData(Message message) {
        switch (message.what) {
            case GET_DEVICE_PROPERTIES:
                String string = (String) message.obj;
                Log.d("zoubo", string);
                String response = JSON.parseObject(string).getString("response");
                JSONObject properties =
                        JSON.parseObject(response).getJSONArray("services").getJSONObject(0).getJSONObject(
                                "properties");
                Log.d("zoubo", properties.toJSONString());
                //设置属性
                setData(properties);
                //开启定时轮询（查询设备属性）
                if (myScheduleTask == null) {
                    myScheduleTask = new MyScheduleTask();
                }
                getMyHttpHandler().postDelayed(myScheduleTask, 2000);
                break;
            case 1:
                String string2 = (String) message.obj;
                Log.d("zoubo", "下发命令响应:" + string2);
                String result_code = JSON.parseObject(string2).getJSONObject("response").getString("result_code");
                Log.d("zoubo", result_code);
                if (result_code.equals("0")) {
                    control.setEnabled(true);
                    //同步状态
                    value4.setText(control.isChecked() ? "开启" : "关闭");
                } else {
                    Toast.makeText(NongyeActivity.this, "设备响应失败", Toast.LENGTH_SHORT).show();
                }
                break;
            case 2:
                String string3 = (String) message.obj;
                Log.d("zoubo", "下发命令响应:" + string3);
                String result_code2 = JSON.parseObject(string3).getJSONObject("response").getString("result_code");
                Log.d("zoubo", result_code2);
                if (result_code2.equals("0")) {
                    control2.setEnabled(true);
                    //同步状态
                    value5.setText(control.isChecked() ? "开启" : "关闭");
                } else {
                    Toast.makeText(NongyeActivity.this, "设备响应失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    /**
     * 设置属性数据
     *
     * @param properties
     */
    private void setData(JSONObject properties) {
        AnimatorSet set = new AnimatorSet();
        set.playTogether(setAnimText(value, properties.getString("Temperature")), setAnimText(value2,
                properties.getString("Humidity")), setAnimText(value3, properties.getString("Luminance")));
        set.start();
        //井盖状态
        value4.setText(properties.getString("LightStatus"));
        value5.setText(properties.getString("MotorStatus"));
    }

    private ValueAnimator setAnimText(TextView target, String value) {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.0f, Float.valueOf(value)).setDuration(1000);
        valueAnimator.setDuration(1000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                target.setText(animation.getAnimatedValue().toString());
            }
        });
        return valueAnimator;
    }

    @Override
    protected String serviceID() {
        return "Agriculture";
    }

    @Override
    protected int layoutID() {
        return R.layout.activity_nongye;
    }

    private TextView value;
    private TextView value2;
    private TextView value3;
    private TextView value4;
    private TextView value5;
    private Switch control;
    private Switch control2;

    @Override
    protected void initView() {
        value = (TextView) findViewById(R.id.value);
        value2 = (TextView) findViewById(R.id.value2);
        value3 = (TextView) findViewById(R.id.value3);
        value4 = (TextView) findViewById(R.id.value4);
        value5 = (TextView) findViewById(R.id.value5);
        control = (Switch) findViewById(R.id.control);
        control2 = (Switch) findViewById(R.id.control2);
        MyControlListener myControlListener = new MyControlListener("Agriculture_Control_light", "Light", 1);
        control.setOnClickListener(myControlListener);
        MyControlListener myControlListener2 = new MyControlListener("Agriculture_Control_Motor", "Motor", 2);
        control2.setOnClickListener(myControlListener2);
    }
}